//
//  ActivityViewController.m
//  DaHaiDouBan
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 Wang Dahai. All rights reserved.
//

#import "ActivityViewController.h"
#import "AppTool.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
@interface ActivityViewController ()<MBProgressHUDDelegate>

@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic,assign) BOOL isUploading;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic, retain) MBProgressHUD *progress;

@end

@implementation ActivityViewController
- (void)dealloc
{
    [_activityTableView release];
    [_hub release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"活动";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    
    
    self.activityTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    
    
    [self.view addSubview:self.activityTableView];
    [_activityTableView release];
    
    self.activityTableView.delegate = self;
    self.activityTableView.dataSource = self;
    
    //让小菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelColor = [UIColor blueColor];
    self.hub.labelFont = [UIFont systemFontOfSize:20];
    self.hub.labelText = @"玩命加载中......";
    
    
    //注册 cell
    [self.activityTableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];

    
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    
    [AppTool getDataWithAFNFromURL:url_string andBlock:^(id result) {
        
        NSMutableDictionary *dic = result;
        
        NSMutableArray *array = [dic objectForKey:@"events"];
        
        self.activityListArray = [NSMutableArray array];
        
        for (NSMutableDictionary *dic in array) {
            
            
            Activity *activity = [[Activity alloc] init];
            
            [activity setValuesForKeysWithDictionary:dic];
            
            [self.activityListArray addObject:activity];
            
            [activity release];
    }
        
        
        [self.activityTableView reloadData];
        //让菊花停止旋转
        [self.hub hide:YES];
        
    }];
    
    self.currentPage = 0;
    self.isUploading = NO;
    
    
    //  上拉加载更多
    [self footerFresh];
    
    //  下拉刷新首页
    [self headerFresh];
}
//下拉刷新
-(void)headerFresh{
    
        [self.activityTableView addHeaderWithCallback:^{
        NSLog(@"下拉");
        self.currentPage = 0;
        self.isUploading = NO;
        
        self.progress = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:self.progress];
        [self.view bringSubviewToFront:self.progress];
        self.progress.mode = MBProgressHUDModeIndeterminate;
        self.progress.delegate = self;
        self.progress.labelText = @"loading...";
        [self.progress show:YES];
        
        
        [self getAFNetWorkingData:self.currentPage];
        
    }];
    
    
    [self.activityTableView headerBeginRefreshing];
}

-(void)footerFresh{
    [self.activityTableView addFooterWithCallback:^{
        NSLog(@"上拉");
        self.currentPage++;
        self.isUploading = YES;
        
        self.progress = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:self.progress];
        [self.view bringSubviewToFront:self.progress];
        self.progress.delegate = self;
        self.progress.labelText = @"loading...";
        [self.progress show:YES];
        
        [self getAFNetWorkingData:self.currentPage];
        
    }];
}

-(void)getAFNetWorkingData:(NSInteger)page{
 
    // AFN
    NSString *url = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php?page=%ld",page];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"请求成功");
        
        NSMutableDictionary *dictionary = responseObject;
        NSArray *array = [dictionary objectForKey:@"events"];
        for (NSDictionary *middleDic in array) {
            
            Activity *activity = [[Activity alloc] init];
            
            [activity setValuesForKeysWithDictionary:middleDic];
            
            //  因为是一个一个的tableViewCell,所以在ActivitiesTableViewCell里面修改
            
            NSMutableDictionary *smallDic = [middleDic objectForKey:@"owner"];
            [activity setValuesForKeysWithDictionary:smallDic];
            [activity release];
            
        }
        
        [self.activityTableView headerEndRefreshing];
        [self.activityTableView footerEndRefreshing];
        [self.activityTableView reloadData];
        
        if (self.progress)
        {
            [self.progress removeFromSuperview];
            [self.progress release];
            self.progress = nil;
        }
        
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
        
        [self.activityTableView headerEndRefreshing];
        [self.activityTableView footerEndRefreshing];
        
        if (self.progress)
        {
            [self.progress removeFromSuperview];
            [self.progress release];
            self.progress = nil;
        }
        
        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.activityListArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    
    Activity *activity = [self.activityListArray objectAtIndex:indexPath.row];
    
    cell.activity = activity;
   // cell.textLabel.text = @"大海";
    
    return cell;
    
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 180;
    
}
//cell 的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailViewController *actiDatailVC = [[ActivityDetailViewController alloc] init];
    

    Activity *activity = [self.activityListArray objectAtIndex:indexPath.row];

    
    actiDatailVC.activity = activity;
    
    [self.navigationController pushViewController:actiDatailVC animated:YES];
    
    [actiDatailVC release];
    
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
